package com.threedt.nova.portfolio.transformer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.integration.core.Message;
import org.springframework.integration.message.MessageBuilder;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.threedt.nova.portfolio.pojo.NewsItem;

public class SyndFeedToNewsItemTransformer implements Serializable {

/*	public Message<List<NewsItem>> transform(Message<SyndFeed> syndFeedMessage) {
        System.out.println("Received a feed from the blog {}"+syndFeedMessage.getPayload().getTitle());

        SyndFeed syndFeed = syndFeedMessage.getPayload();

        List<NewsItem> newsItems = new ArrayList<NewsItem>();
        List syndFeedItems = syndFeed.getEntries();
        for (Object syndFeedEntry:syndFeedItems) {
            SyndEntry syndEntry = (SyndEntry)syndFeedEntry;
            String title = syndEntry.getTitle();
            String author = syndEntry.getAuthor();
            String description = syndEntry.getDescription().getValue();
            SyndContent syndContent = (SyndContent)syndEntry.getContents().get(0);
            String content = syndContent.getValue();
            
            NewsItem newsItem = new NewsItem();
            newsItem.setHeader(title);
            newsItem.setBody(content);
            System.out.println("The author is "+author);
            System.out.println("The content is "+content);
            // a lot of other information is possible
            
            newsItems.add(newsItem);
        }
        Message<List<NewsItem>> newMessage = MessageBuilder.withPayload(newsItems)
                .copyHeaders(syndFeedMessage.getHeaders()).build();
        return newMessage;
    }*/
	
	
	public List<NewsItem> transformNew(Message<SyndFeed> syndFeedMessage) {

        SyndFeed syndFeed = syndFeedMessage.getPayload();

        List<NewsItem> newsItems = new ArrayList<NewsItem>();
        List<SyndEntry> syndFeedItems = syndFeed.getEntries();
        if(syndFeedItems == null ){
        	return newsItems;
        }
        for (SyndEntry syndEntry : syndFeedItems) {
            String title = syndEntry.getTitle();
            String author = syndEntry.getAuthor();
            String description = syndEntry.getDescription().getValue();
            NewsItem newsItem = new NewsItem();
            newsItem.setHeader(title);
            
            if(syndEntry.getContents()!= null  && syndEntry.getContents().size() > 0 ){
            	SyndContent syndContent = (SyndContent)syndEntry.getContents().get(0);
            	String content = syndContent.getValue();
            	newsItem.setBody(content);
            }else{
            	newsItem.setBody("Update Time is :"+new Date()+" \n The author is "+author +"\n The description is "+description);
            }
            
           
            
            newsItems.add(newsItem);
        }
       
        return newsItems;
    }

	
}
